#!/bin/bash
# 刘云派球教育平台 - 环境安装脚本
# Linux/macOS 系统使用

echo "===================================="
echo "刘云派球教育平台 - 环境安装"
echo "===================================="

# 检查是否存在Python3
if ! command -v python3 &> /dev/null; then
    echo "错误：未找到Python3，请先安装Python 3.8+"
    echo "Ubuntu/Debian: sudo apt install python3 python3-venv python3-pip"
    echo "CentOS/RHEL: sudo yum install python3 python3-pip"
    exit 1
fi

echo "当前Python版本："
python3 --version

# 进入后端目录
cd backed

# 检查虚拟环境是否已存在
if [ -d "venv" ]; then
    echo "虚拟环境已存在，跳过创建步骤..."
else
    echo "正在创建Python虚拟环境..."
    python3 -m venv venv
    if [ $? -ne 0 ]; then
        echo "错误：虚拟环境创建失败"
        exit 1
    fi
    echo "虚拟环境创建成功！"
fi

# 激活虚拟环境并安装依赖
echo "正在激活虚拟环境..."
source venv/bin/activate

# 升级pip
echo "正在升级pip..."
python -m pip install --upgrade pip

# 安装依赖
echo "正在安装项目依赖..."
if [ -f "requirements.txt" ]; then
    pip install -r requirements.txt
    if [ $? -ne 0 ]; then
        echo "错误：依赖安装失败"
        exit 1
    fi
    echo "后端依赖安装完成！"
else
    echo "警告：未找到requirements.txt文件"
fi

# 返回根目录
cd ..

echo "===================================="
echo "🎉 环境安装完成！"
echo "要启动开发环境，请运行：./start-dev.sh"
echo "===================================="
